package com.sgb.kjwl.view.ui.fragment.self;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentLeaseOrderStateBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.lease.LeaseOrderListEntity;
import com.sgb.kjwl.view.event.LeaseOrderEvent;
import com.sgb.kjwl.view.ui.adapter.self.publish.LeaseOrderStateAdapter;
import com.sgb.kjwl.viewmodel.self.publish.LeaseOrderStateModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

/**
 * 租赁-订单列表
 */
public class LeaseOrderStateFragment extends AppLazyFragment {
    private FragmentLeaseOrderStateBinding binding;
    @Inject
    LeaseOrderStateModel viewModel;
    private String status = ""; //订单状态status 1待接单,2 待确认,3待付款，4支付中，5待运输，6待验收,7已完成,8已取消 ,9已拒绝,全部为空
    private String type;//1=求租,2=出租
    private LeaseOrderStateAdapter adapter;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    public static LeaseOrderStateFragment newInstance(String status, String type) {
        LeaseOrderStateFragment fragment = new LeaseOrderStateFragment();
        Bundle bundle = new Bundle();
        bundle.putString("status", status);
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_lease_order_state, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    @Override
    protected void initView() {
        iniArguments();
        if (adapter == null) {
            adapter = new LeaseOrderStateAdapter(getActivity(), type);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvView.setLayoutManager(layoutManager);
        binding.rlvView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getResources().getDimension(R.dimen.dp1), getResources().getColor(R.color.color_F5F5F5)));
        binding.rlvView.setAdapter(adapter);
        binding.rlvView.setLoadingMoreEnabled(true);
        binding.rlvView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
        adapter.setItemCallBack(new LeaseOrderStateAdapter.ItemCallBack() {


            @Override
            public void detail(String id,String module) {
                viewModel.getH5Url(id, type,module);
            }

            @Override
            public void onDelete(String id) {
                optDelState(id);
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        loadData(false);
    }

    private void iniArguments() {
        status = (String) getArguments().get("status");
        type = (String) getArguments().get("type");
    }

    private void loadData(boolean isLoadMore) {
        viewModel.map.get().put("status", status);
        viewModel.map.get().put("type", type);
        viewModel.getLeaseOrderListModel(isLoadMore);
    }

    public void showList(List<LeaseOrderListEntity> datas, boolean isLoadMore) {
        if (isLoadMore) {
            binding.rlvView.loadMoreComplete();
            if (datas.size() != 0) {
                adapter.addDatas(datas);
            }
        } else {
            if (datas.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.rlvView.refreshComplete();
            binding.rlvView.setLoadingMoreEnabled(true);
            adapter.setDatas(datas);
        }
    }

    public void onLoadComplete(boolean isLoadMore) {
        if (isLoadMore) {
            binding.rlvView.loadMoreComplete();
        } else {
            binding.rlvView.refreshComplete();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseOrderEvent event) {//刷新
        loadData(false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void optDelState(String id) {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("确定要删除订单吗？")
                .setMsg("删除后可以从电脑端订单回收站恢复")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        viewModel.deleteOrder(id,type);
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();

    }
}
